﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using WeiSha.Core.Param.Method;

namespace WeiSha.Core
{
	// Token: 0x02000011 RID: 17
	public class DataConvert
	{
		// Token: 0x06000074 RID: 116 RVA: 0x00004760 File Offset: 0x00002960
		public static object ChangeType(string value, Type type)
		{
			bool flag = type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>));
			if (flag)
			{
				bool flag2 = value == null || value.ToString().Length == 0;
				if (flag2)
				{
					return null;
				}
				NullableConverter nullableConverter = new NullableConverter(type);
				type = nullableConverter.UnderlyingType;
			}
			return DataConvert.ToObject(type, value);
		}

		// Token: 0x06000075 RID: 117 RVA: 0x000047CC File Offset: 0x000029CC
		public static object ToObject(Type type, string actual)
		{
			object obj = null;
			string name = type.Name;
			string text = name;
			uint num = PrivateImplementationDetails.ComputeStringHash(text);
            if (num <= 2386971688U)
			{
				if (num <= 765439473U)
				{
					if (num != 423635464U)
					{
						if (num != 697196164U)
						{
							if (num != 765439473U)
							{
								goto IL_036C;
							}
							if (!(text == "Int16"))
							{
								goto IL_036C;
							}
						}
						else if (!(text == "Int64"))
						{
							goto IL_036C;
						}
					}
					else if (!(text == "SByte"))
					{
						goto IL_036C;
					}
				}
				else if (num <= 1324880019U)
				{
					if (num != 1323747186U)
					{
						if (num != 1324880019U)
						{
							goto IL_036C;
						}
						if (!(text == "UInt64"))
						{
							goto IL_036C;
						}
					}
					else if (!(text == "UInt16"))
					{
						goto IL_036C;
					}
				}
				else if (num != 1615808600U)
				{
					if (num != 2386971688U)
					{
						goto IL_036C;
					}
					if (!(text == "Double"))
					{
						goto IL_036C;
					}
				}
				else
				{
					if (!(text == "String"))
					{
						goto IL_036C;
					}
					return (actual == null) ? "" : actual.Trim();
				}
			}
			else if (num <= 2779444460U)
			{
				if (num != 2615964816U)
				{
					if (num != 2711245919U)
					{
						if (num != 2779444460U)
						{
							goto IL_036C;
						}
						if (!(text == "Decimal"))
						{
							goto IL_036C;
						}
					}
					else if (!(text == "Int32"))
					{
						goto IL_036C;
					}
				}
				else
				{
					if (!(text == "DateTime"))
					{
						goto IL_036C;
					}
					bool flag = !string.IsNullOrWhiteSpace(actual);
					if (flag)
					{
						bool flag2 = actual.IndexOf('.') > -1;
						if (flag2)
						{
							actual = actual.Substring(0, actual.LastIndexOf('.'));
						}
						Regex regex = new Regex("^\\d+$");
						bool flag3 = regex.IsMatch(actual.Trim());
						if (flag3)
						{
							long num2 = 0L;
							long.TryParse(actual, out num2);
							obj = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddMilliseconds((double)num2);
						}
						else
						{
							try
							{
								obj = DateTime.ParseExact(actual, "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);
							}
							catch
							{
								obj = DateTime.ParseExact(actual, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
							}
						}
					}
					return obj;
				}
			}
			else if (num <= 3538687084U)
			{
				if (num != 3409549631U)
				{
					if (num != 3538687084U)
					{
						goto IL_036C;
					}
					if (!(text == "UInt32"))
					{
						goto IL_036C;
					}
				}
				else if (!(text == "Byte"))
				{
					goto IL_036C;
				}
			}
			else if (num != 3969205087U)
			{
				if (num != 4051133705U)
				{
					goto IL_036C;
				}
				if (!(text == "Single"))
				{
					goto IL_036C;
				}
			}
			else
			{
				if (!(text == "Boolean"))
				{
					goto IL_036C;
				}
				bool flag4 = actual == null;
				if (flag4)
				{
					obj = null;
				}
				else
				{
					bool flag5 = actual.ToLower() == "true";
					if (flag5)
					{
						obj = true;
					}
					else
					{
						bool flag6 = actual.ToLower() == "false";
						if (flag6)
						{
							obj = false;
						}
						else
						{
							obj = null;
						}
					}
				}
				return obj;
			}
			bool flag7 = string.IsNullOrWhiteSpace(actual);
			if (flag7)
			{
				actual = "0";
			}
			return Convert.ChangeType(actual, type);
			IL_036C:
			obj = Convert.ChangeType(actual, type);
			return obj;
		}

		// Token: 0x06000076 RID: 118 RVA: 0x00004B68 File Offset: 0x00002D68
		public static ConvertToAnyValue ChangeType(object value)
		{
			return new ConvertToAnyValue(value);
		}

		// Token: 0x06000077 RID: 119 RVA: 0x00004B80 File Offset: 0x00002D80
		public static DataTable ToDataTable<T>(List<T> list)
		{
			return DataConvert.ToDataTable<T>(list, string.Empty);
		}

		// Token: 0x06000078 RID: 120 RVA: 0x00004BA0 File Offset: 0x00002DA0
		public static DataTable ToDataTable<T>(List<T> list, string primaryKey)
		{
			bool flag = list.Count < 1;
			DataTable dataTable;
			if (flag)
			{
				dataTable = null;
			}
			else
			{
				T t = list[0];
				Type type = t.GetType();
				PropertyInfo[] properties = type.GetProperties();
				DataTable dataTable2 = new DataTable(typeof(T).Name);
				for (int i = 0; i < properties.Length; i++)
				{
					dataTable2.Columns.Add(properties[i].Name, properties[i].PropertyType);
					bool flag2 = properties[i].Name.Equals(primaryKey, StringComparison.CurrentCultureIgnoreCase);
					if (flag2)
					{
						bool flag3 = dataTable2.PrimaryKey == null || dataTable2.PrimaryKey.Length < 1;
						if (flag3)
						{
							dataTable2.PrimaryKey = new DataColumn[] { dataTable2.Columns[properties[i].Name] };
						}
					}
				}
				foreach (T t2 in list)
				{
					object obj = t2;
					bool flag4 = obj.GetType() != type;
					if (flag4)
					{
						throw new Exception("要转换的集合元素类型不一致");
					}
					object[] array = new object[properties.Length];
					for (int j = 0; j < properties.Length; j++)
					{
						array[j] = properties[j].GetValue(obj, null);
					}
					dataTable2.Rows.Add(array);
				}
				dataTable = dataTable2;
			}
			return dataTable;
		}

		// Token: 0x06000079 RID: 121 RVA: 0x00004D34 File Offset: 0x00002F34
		public static string MD5(string val)
		{
			bool flag = string.IsNullOrWhiteSpace(val);
			string text;
			if (flag)
			{
				text = "";
			}
			else
			{
				bool flag2 = string.IsNullOrWhiteSpace(val.ToString());
				if (flag2)
				{
					text = "";
				}
				else
				{
					MD5 md = global::System.Security.Cryptography.MD5.Create();
					byte[] array = md.ComputeHash(Encoding.UTF8.GetBytes(val.ToString()));
					string text2 = string.Empty;
					for (int i = 0; i < array.Length; i++)
					{
						text2 += array[i].ToString("x2");
					}
					text = text2;
				}
			}
			return text;
		}

		// Token: 0x0600007A RID: 122 RVA: 0x00004DCC File Offset: 0x00002FCC
		public static string EncryptForDES(string encryptStr, string encryptKey)
		{
			byte[] array = new byte[] { 18, 52, 86, 120, 144, 171, 205, 239 };
			string text;
			try
			{
				bool flag = string.IsNullOrWhiteSpace(encryptKey);
				if (flag)
				{
					encryptKey = "";
				}
				bool flag2 = encryptKey.Length < 8;
				if (flag2)
				{
					encryptKey += "12345678";
				}
				bool flag3 = encryptKey.Length > 8;
				if (flag3)
				{
					encryptKey = encryptKey.Substring(0, 8);
				}
				byte[] bytes = DataConvert.encoding.GetBytes(encryptKey);
				DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
				byte[] bytes2 = DataConvert.encoding.GetBytes(encryptStr);
				MemoryStream memoryStream = new MemoryStream();
				CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateEncryptor(bytes, array), CryptoStreamMode.Write);
				cryptoStream.Write(bytes2, 0, bytes2.Length);
				cryptoStream.FlushFinalBlock();
				text = Convert.ToBase64String(memoryStream.ToArray());
			}
			catch
			{
				text = null;
			}
			return text;
		}

		// Token: 0x0600007B RID: 123 RVA: 0x00004EAC File Offset: 0x000030AC
		public static string DecryptForDES(string decryptStr, string decryptKey)
		{
			string text;
			try
			{
				byte[] array = new byte[] { 18, 52, 86, 120, 144, 171, 205, 239 };
				byte[] array2 = new byte[decryptStr.Length];
				bool flag = decryptKey.Length > 8;
				if (flag)
				{
					decryptKey = decryptKey.Substring(0, 8);
				}
				byte[] bytes = DataConvert.encoding.GetBytes(decryptKey);
				DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
				array2 = Convert.FromBase64String(decryptStr);
				MemoryStream memoryStream = new MemoryStream();
				CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateDecryptor(bytes, array), CryptoStreamMode.Write);
				cryptoStream.Write(array2, 0, array2.Length);
				cryptoStream.FlushFinalBlock();
				text = DataConvert.encoding.GetString(memoryStream.ToArray());
			}
			catch
			{
				text = null;
			}
			return text;
		}

		// Token: 0x0600007C RID: 124 RVA: 0x00004F6C File Offset: 0x0000316C
		public static string EncryptForRSA(string encryptStr, string encryptKey)
		{
			RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider(1024, new CspParameters
			{
				Flags = CspProviderFlags.UseMachineKeyStore
			});
			byte[] bytes = DataConvert.encoding.GetBytes(encryptStr);
			byte[] array = rsacryptoServiceProvider.Encrypt(bytes, false);
			return Convert.ToBase64String(array);
		}

		// Token: 0x0600007D RID: 125 RVA: 0x00004FB8 File Offset: 0x000031B8
		public static string DecryptForRSA(string decryptStr, string decryptKey)
		{
			bool flag = string.IsNullOrWhiteSpace(decryptStr);
			string text;
			if (flag)
			{
				text = string.Empty;
			}
			else
			{
				RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider(1024, new CspParameters
				{
					Flags = CspProviderFlags.UseMachineKeyStore
				});
				byte[] array = Convert.FromBase64String(decryptStr);
				byte[] array2 = rsacryptoServiceProvider.Decrypt(array, false);
				text = DataConvert.encoding.GetString(array2);
			}
			return text;
		}

		// Token: 0x0600007E RID: 126 RVA: 0x00005018 File Offset: 0x00003218
		public static string EncryptForBase64(string encryptStr)
		{
			byte[] bytes = DataConvert.encoding.GetBytes(encryptStr);
			return Convert.ToBase64String(bytes);
		}

		// Token: 0x0600007F RID: 127 RVA: 0x0000503C File Offset: 0x0000323C
		public static string EncryptForBase64UrlEncode(string encryptStr)
		{
			byte[] bytes = DataConvert.encoding.GetBytes(encryptStr);
			string text = Convert.ToBase64String(bytes);
			return HttpUtility.UrlEncode(text);
		}

		// Token: 0x06000080 RID: 128 RVA: 0x00005068 File Offset: 0x00003268
		public static string DecryptForBase64(string decryptStr)
		{
			byte[] array = Convert.FromBase64String(decryptStr);
			return DataConvert.encoding.GetString(array);
		}

		// Token: 0x04000024 RID: 36
		private static Encoding encoding = Encoding.UTF8;
	}
}
